#-*- encoding:utf-8 -*-
'''
Created on 2010-12-27

@author: 屠克
'''
from flask import Module,session,request,redirect,url_for,render_template
from Models import db
from Utils import operation,func

adminview = Module(__name__,static_path='/../static')

@adminview.route('/')
@operation.user.Authorize
def index():
    '''
    管理首页
    '''
    return render_template('admin/index.html')

@adminview.route('/login',methods=['POST','GET'])
def login():
    '''
    后台登陆
    '''
    if(request.method == 'POST'):
        loginrs = operation.user.Login(request.form.get('username'),request.form.get('password'))
        if loginrs[0] == True:
            return redirect(url_for('index'))
        else:
            return loginrs[1]
    return render_template('admin/login.html')

@adminview.route('/config',methods=['POST','GET'])
@operation.user.Authorize
def config():
    '''
    网站配置
    '''
    if(request.method == 'POST'):
        if(operation.config.Update(request.form['config'],request.form['value'])[0] == True):
            return func.msg(u"修改成功", url_for("config"))
        else:
            return func.msg(u"修改失败", url_for("config"))

    d = {}
    d['configs'] = operation.config.GetAll()
    if d['configs'][0] == False:
        d['configs'] = []
    else:
        d['configs'] = d['configs'][1]
    return render_template('admin/config.html',**d)


@adminview.route('/categories-add',methods=['POST','GET'])
@operation.user.Authorize
def categories_add():
    '''
    添加类别
    '''
    if(request.method == 'POST'):
        rs = operation.categories.Add(request.form['title'])
        if(rs[0] == True):
            return func.msg(u"添加成功", url_for("categories_list"))
        else:
            return func.msg(u"添加失败，%s"%(rs[1]), url_for("categories_add"))
    return render_template('admin/categories-add.html')
    
@adminview.route('/categories')
@operation.user.Authorize
def categories_list():
    '''
    类别列表
    '''
    d = {}
    d['categories'] = operation.categories.GetAll()
    if d['categories'][0] == False:
        d['categories'] = []
    else:
        d['categories'] = d['categories'][1]
        
    return render_template('admin/categories-list.html',**d)

@adminview.route('/categories-update/<int:id>',methods=['POST','GET'])
@operation.user.Authorize
def categories_update(id):
    '''
    更新类别
    @param id:类别ID
    '''
    if(request.method == 'POST'):
        rs = operation.categories.Update(id, request.form['title'])
        if rs[0] == True:
            return func.msg(u"修改成功", url_for("categories_list"))
        else:
            return func.msg(u"修改失败，%s"%(rs[1]), url_for("categories_update",id=id))
    d={}
    rs = operation.categories.Get(id)
    if rs[0] == False:
        return func.msg(u"%s"%(rs[1]), url_for("categories_list"))
    
    d['title']=rs[1][0].title
    return render_template('admin/categories-update.html',**d)
    

@adminview.route('/categories-delete/<int:id>')
@operation.user.Authorize
def categories_delete(id):
    '''
    删除类别
    @param id:类别ID
    '''
    rs = operation.categories.Delete(id)
    if rs[0] == True:
        return func.msg(u"删除成功", url_for("categories_list"))
    else:
        return func.msg(u"删除失败，%s"%(rs[1]), url_for("categories_list"))

@adminview.route('/tag')
@operation.user.Authorize
def tag_list():
    '''
    类别列表
    '''
    d = {}
    d['tag'] = operation.tag.GetAll()
    if d['tag'][0] == False:
        d['tag'] = []
    else:
        d['tag'] = d['tag'][1]
        
    return render_template('admin/tag-list.html',**d)

@adminview.route('/tag-delete/<int:id>')
@operation.user.Authorize
def tag_delete(id):
    '''
    删除tag
    @param id:tagID
    '''
    rs = operation.tag.Delete(id)
    if rs[0] == True:
        return func.msg(u"删除成功", url_for("tag_list"))
    else:
        return func.msg(u"删除失败，%s"%(rs[1]), url_for("tag_list"))
    
@adminview.route('/article-add',methods=['POST','GET'])
@operation.user.Authorize
def article_add():
    '''
    添加类别
    '''
    if(request.method == 'POST'):
        return ''
    return render_template('admin/article-add.html')
